package cn.xuchunh.campusbbs.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.api.ApiConstant;
import cn.xuchunh.campusbbs.base.BaseActivity;
import cn.xuchunh.campusbbs.base.BaseFragment.OnFragmentInteractionListener;
import cn.xuchunh.campusbbs.bean.Topic;
import cn.xuchunh.campusbbs.net.HttpRequestManager;
import cn.xuchunh.campusbbs.storage.sqlite.DBManager;
import cn.xuchunh.campusbbs.ui.fragments.TopicReplyListlFragment;
import cn.xuchunh.campusbbs.ui.fragments.TopicReplyPagerFragment;
import cn.xuchunh.campusbbs.utils.IntentParamsKey;

/**
 * Created by XuChunH on 2015/9/14.
 */
public class
TopicDetailActivity extends BaseActivity implements OnFragmentInteractionListener {

    public static final int ACTION_REPLY = 0;
    public static final int ACTION_REPLY_DETAIL = 1;

    private Topic topic;

    private TopicReplyListlFragment topicDetailFragment;
    private TopicReplyPagerFragment topicReplyPagerFragment;

    public static void launch(Context context, Topic topic) {
        if (topic == null) {
            return;
        }
        Intent intent = new Intent(context, TopicDetailActivity.class);
        intent.putExtra(IntentParamsKey.TOPIC, topic);
        context.startActivity(intent);
    }

    @Override
    protected int createContentRes() {
        return R.layout.activity_topic_detail;
    }

    @Override
    protected void initView(View view) {
        setTitle(topic.getForumName());
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.container, topicDetailFragment, TopicReplyListlFragment.class.getSimpleName());
        ft.commit();
    }

    @Override
    protected void beforeInitView() {
        topic = (Topic) getIntent().getSerializableExtra(IntentParamsKey.TOPIC);
        if (topic == null) {
            finish();
        }
        topicDetailFragment = TopicReplyListlFragment.getInstance(topic);
        setShowHomeUp(true);

        String cookies = HttpRequestManager.getInstance(this).getCookies();
        if(!TextUtils.isEmpty(cookies)){
            CookieSyncManager.createInstance(this);
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            cookieManager.setCookie("http://" + ApiConstant.SERVER_DOMAIN, cookies);
            CookieSyncManager.getInstance().sync();
        }

    }

    @Override
    protected void onStop() {
        DBManager.close();
        super.onStop();
    }

    @Override
    public void onFragmentInteraction(Bundle data) {
        if (data != null) {
            int action = data.getInt(IntentParamsKey.ACTION);
            if (action == ACTION_REPLY) {
                showReplyList();
            } else if (action == ACTION_REPLY_DETAIL) {
                showReplyDetail(data.getInt(IntentParamsKey.CURRENT, 0), data.getInt(IntentParamsKey.PAGE, 1));
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            if (topicReplyPagerFragment != null && topicReplyPagerFragment.isAdded()) {
                showReplyList();
            } else {
                finish();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if (topicReplyPagerFragment != null && topicReplyPagerFragment.isAdded()) {
            showReplyList();
        } else {
            super.onBackPressed();
        }
    }

    private void showReplyList() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (topicReplyPagerFragment != null && topicReplyPagerFragment.isAdded()) {
            ft.remove(topicReplyPagerFragment);
            topicReplyPagerFragment = null;
            ft.show(topicDetailFragment);
            ft.commit();
        }
    }

    private void showReplyDetail(int position, int page) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        topicReplyPagerFragment = TopicReplyPagerFragment.newInstance(topic, position, page);
        ft.hide(topicDetailFragment);
        ft.add(R.id.container, topicReplyPagerFragment);
        ft.commit();
    }

}
